/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
/*
 * log.c
 * Copyright (C) Francesco Marcantoni 2006 <ramyel@gmail.com>
 * 
 * You may redistribute it and/or modify it under the terms of the
 * GNU General Public License, as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option)
 * any later version.
 * 
 * main.c is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with main.c.  If not, write to:
 * 	The Free Software Foundation, Inc.,
 * 	51 Franklin Street, Fifth Floor
 * 	Boston, MA  02110-1301, USA.
 */

#include "../include/tokyo.h"

// ------
// Funzione di log
// ------
void tlog (LogPriority priority, gchar *function, gchar *str, ... )
{
	gchar buftime [50];
	gchar buf [MSL];
	gchar buflog [MSL];
	va_list param;
	time_t ttime;
	GDate* gdate;
	GTime gtime;

	g_return_if_fail (priority < P_MAX);
	g_return_if_fail (*function != '\0');
	g_return_if_fail (*str != '\0');
	
	// Ricavo l'ora attuale
	time (&ttime);
	gtime = (GTime)ttime;
	gdate = g_date_new ();
	g_date_set_time (gdate, gtime);
	g_date_strftime (buftime, 50, "%D", gdate);
	
	// Compongo il Buffer
	va_start (param, str);
	g_vsnprintf (buf, MSL, str, param );
	va_end 	 (param);
	
	switch (priority) 
	{
		case P_NORMAL :
			g_snprintf (buflog, MSL, _("%s: (Normal) %s -> %s"), buftime, function, buf);
			g_print ("%s: [%s] %s\n", TOKYO_NAME_PROGRAM, buftime, buf);
		break;

		case P_DEBUG :
			if (TOKYO_DEBUG) 
			{
				g_snprintf (buflog, MSL, _("%s: (Debug) %s -> %s"), buftime, function, buf);
				g_print ("%s: [%s] (%s) %s\n", TOKYO_NAME_PROGRAM, buftime, function, buf);
			}
		break;

		case P_ERROR :
			g_snprintf (buflog, MSL, _("%s: (Error) %s -> %s"), buftime, function, buf);
			g_print ("%s: [%s] (%s) %s\n", TOKYO_NAME_PROGRAM, buftime, function, buf);
		break;

		default :
			g_print ("Default-> %s: [%s] (%s) %s\n", TOKYO_NAME_PROGRAM, buftime, function, buf);
	}
	
	log_list_append (priority, function, buflog);
}

// ------
// Appende un nuovo log alla lista TOKYO_LOG_LIST
// ------
void log_list_append (LogPriority priority, gchar *function, gchar *log)
{
	Log *new_log;

	g_return_if_fail (priority < P_MAX);
	g_return_if_fail (*function != '\0');
	g_return_if_fail (*log != '\0');

	new_log = g_new (Log, 1);
	new_log->log = g_strdup (log);
	new_log->function = g_strdup (function);
	new_log->priority = priority;
	
	TOKYO_LOG_LIST = g_list_append (TOKYO_LOG_LIST, new_log);
}

// ------
// FREE STRUCT Log
// ------
void log_free (Log *log)
{
	g_return_if_fail (log != NULL);

	g_free (log->log);
	g_free (log->function);
}

// ------
// FREE GLIST TOKYO_LOG_LIST
// ------
void log_list_free (void)
{
	GList *list = NULL;

	for (list = g_list_first(TOKYO_LOG_LIST); list; list = g_list_next(list))
		if (LIST_DATA (Log, list))
			log_free (LIST_DATA (Log, list));

	g_list_free (TOKYO_LOG_LIST);
	TOKYO_LOG_LIST = NULL;
}
